import Vue from "vue";
import Vuex from "vuex";
import getters from "./getters.js";
import { getUserInfo } from '@/api/modules/base.js'


Vue.use(Vuex);
export default new Vuex.Store({
  state: {
    userInfo: {
      tmb_id: '',
      tmb_phone: '',
      tmb_company_name: '',
      tmb_position: '',
      tmb_icon: ''
    }, // 用户信息

    linkUser: {
      name: '',
      account: ''
    },

    tabbarRules: '1:1,2:1,3:1,4:1,5:1'
  },
  getters: getters,
  mutations: {
    SET_USER_INFO(state, payload) {
      Object.keys(payload).forEach(key => {
        Vue.prototype.$set(state.userInfo, key, payload[key])
      })
      // Object.assign(state.userInfo, payload);
    },

    SET_LINK_USER(state, payload) {
      Object.assign(state.linkUser, payload);
    },

    SET_TABBAR_RULES(state, payload){
      state.tabbarRules = payload || state.tabbarRules
    }
  },
  actions: {
    async getMemberInfo({ state, commit }) {
      try {
        const res = await getUserInfo()
        commit("SET_USER_INFO", res.tbMemberInfo)
        commit("SET_LINK_USER", res.person_is ? res.person_is : {})
      } catch (e) {
      }
    },

    loginOut({ commit }) {
      commit('SET_USER_INFO', {
        tmb_id: '',
        tmb_phone: '',
        tmb_company_name: '',
        tmb_position: '',
        tmb_icon: ''
      })
      commit('SET_LINK_USER', {
        name: '',
        account: ''
      })
    }
  },
});
